<%--
/**
 * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 */
--%>

<%@ include file="/html/portlet/asset_publisher/init.jsp" %>

<%
List results = (List)request.getAttribute("view.jsp-results");

int assetEntryIndex = ((Integer)request.getAttribute("view.jsp-assetEntryIndex")).intValue();

AssetEntry assetEntry = (AssetEntry)request.getAttribute("view.jsp-assetEntry");
AssetRendererFactory assetRendererFactory = (AssetRendererFactory)request.getAttribute("view.jsp-assetRendererFactory");
AssetRenderer assetRenderer = (AssetRenderer)request.getAttribute("view.jsp-assetRenderer");

boolean show = ((Boolean)request.getAttribute("view.jsp-show")).booleanValue();

request.setAttribute("view.jsp-showIconLabel", true);

String title = (String)request.getAttribute("view.jsp-title");

if (Validator.isNull(title)) {
	title = assetRenderer.getTitle(locale);
}
PortletURL viewFullContentURL = renderResponse.createRenderURL();

viewFullContentURL.setParameter("struts_action", "/asset_publisher/view_content");
viewFullContentURL.setParameter("assetEntryId", String.valueOf(assetEntry.getEntryId()));
viewFullContentURL.setParameter("type", assetRendererFactory.getType());

if (Validator.isNotNull(assetRenderer.getUrlTitle())) {
	if (assetRenderer.getGroupId() != scopeGroupId) {
		viewFullContentURL.setParameter("groupId", String.valueOf(assetRenderer.getGroupId()));
	}
	viewFullContentURL.setParameter("urlTitle", assetRenderer.getUrlTitle());
}
String viewURL = null;
if (viewInContext) {
	String viewFullContentURLString = viewFullContentURL.toString();
	viewFullContentURLString = HttpUtil.setParameter(viewFullContentURLString, "redirect", currentURL);
	viewURL = assetRenderer.getURLViewInContext(liferayPortletRequest, liferayPortletResponse, viewFullContentURLString);
	viewURL = HttpUtil.setParameter(viewURL, "redirect", currentURL);
}
else {
	viewURL = viewFullContentURL.toString();
}
if (Validator.isNull(viewURL)) {
	viewURL = viewFullContentURL.toString();
}
String viewURLMessage = viewInContext ? assetRenderer.getViewInContextMessage() : "read-more-x-about-x";
viewURL = _checkViewURL(viewURL, currentURL, themeDisplay);

if(Validator.isNotNull(viewURL)) {
	String instanceId = "detail";
	if(instanceId != null) {
		viewURL = viewURL.replaceFirst(layout.getFriendlyURL(), "/chi-tiet");
		viewURL = viewURL.replaceFirst(portletDisplay.getInstanceId(), instanceId);
	}
}

String cssClass = (assetEntryIndex == 0) ? "asset-item" : "dropdown-menu";
%>

<c:if test="<%= show %>">

	<%
	String path = assetRenderer.render(renderRequest, renderResponse, AssetRenderer.TEMPLATE_ABSTRACT);
	request.setAttribute(WebKeys.ASSET_RENDERER, assetRenderer);
	request.setAttribute(WebKeys.ASSET_PUBLISHER_ABSTRACT_LENGTH, abstractLength);
	
	JournalArticle article = (JournalArticle)request.getAttribute(WebKeys.JOURNAL_ARTICLE);
	com.liferay.portlet.journal.model.JournalArticleResource articleResource = com.liferay.portlet.journal.service.JournalArticleResourceLocalServiceUtil.getArticleResource(article.getResourcePrimKey());
	String languageId = LanguageUtil.getLanguageId(request);
	boolean workflowAssetPreview = GetterUtil.getBoolean((Boolean)request.getAttribute(WebKeys.WORKFLOW_ASSET_PREVIEW));
	com.liferay.portlet.journal.model.JournalArticleDisplay articleDisplay = null;
	if (!workflowAssetPreview && article.isApproved()) {
		articleDisplay = com.liferay.portlet.journalcontent.util.JournalContentUtil.getDisplay(articleResource.getGroupId(), articleResource.getArticleId(), null, null, languageId, themeDisplay);
	}
	else {
		articleDisplay = com.liferay.portlet.journal.service.JournalArticleLocalServiceUtil.getArticleDisplay(article, null, null, languageId, 1, null, themeDisplay);
	}
	
	String summary = HtmlUtil.escape(articleDisplay.getDescription());
	if (Validator.isNull(summary)) {
		summary = HtmlUtil.stripHtml(articleDisplay.getContent());
	}
	%>
	
	<c:if test="<%= assetEntryIndex > 0 %>">
		<div class="asset-item">
		<c:choose>
			<c:when test="<%= Validator.isNotNull(viewURL) %>">
				<a class="asset-link btn-link" href="<%= viewURL %>"><%= HtmlUtil.escape(title) %></a>
			</c:when>
			<c:otherwise>
				<%= HtmlUtil.escape(title) %>
			</c:otherwise>
		</c:choose>
	</c:if>
	
	<div class="clearfix <%= cssClass %>">
		<liferay-util:include page="/html/portlet/asset_publisher/asset_actions.jsp" />
		<c:if test="<%= articleDisplay.isSmallImage() %>">
			<%
			String src = StringPool.BLANK;
			if (Validator.isNotNull(articleDisplay.getSmallImageURL())) src = articleDisplay.getSmallImageURL();
			else src = themeDisplay.getPathImage() + "/journal/article?img_id=" + articleDisplay.getSmallImageId() + "&t=" + WebServerServletTokenUtil.getToken(articleDisplay.getSmallImageId()) ;
			%>
			<div class="asset-small-image">
				<img alt="" class="asset-small-image" src="<%= HtmlUtil.escape(src) %>" width="150" />
			</div>
		</c:if>
		<h4 class="asset-title">
			<c:choose>
				<c:when test="<%= Validator.isNotNull(viewURL) %>">
					<a href="<%= viewURL %>"><%= HtmlUtil.escape(title) %></a>
				</c:when>
				<c:otherwise>
					<%= HtmlUtil.escape(title) %>
				</c:otherwise>
			</c:choose>
		</h4>
		<div class="asset-content">
			<div class="asset-summary">
				<%= StringUtil.shorten(summary, abstractLength) %>
			</div>
			<c:if test="<%= assetEntryIndex == 0 && Validator.isNotNull(viewURL) %>">
				<div class="asset-more">
					<a class="btn-link" href="<%= viewURL %>"><liferay-ui:message arguments='<%= new Object[] {"aui-helper-hidden-accessible", HtmlUtil.escape(assetRenderer.getTitle(locale))} %>' key="<%= viewURLMessage %>" /> &raquo; </a>
				</div>
			</c:if>
		</div>
	</div>
	<c:if test="<%= assetEntryIndex > 0 %>">
		</div>
	</c:if>
	<c:if test="<%= (assetEntryIndex + 1) == results.size() %>">
		<div class="final-separator"></div>
	</c:if>
</c:if>